<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.name" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" v-model="addressData.phone" placeholder="收货人手机号码" placeholder-class="placeholder" />
		</view>
		<view class="row b-b" @click="showAddrPicker = true">
			<text class="tit">地区</text>
			<input class="input" type="text" v-model="addressData.posName" placeholder="收货人所在地区" placeholder-class="placeholder" />
		</view>
		
		<view class="row b-b" @click="chooseLocation">
			<text class="tit">详细地址</text>
			<text class="input">
				{{addressData.posAddress}}
			</text>
			<u-icon name="map"></u-icon>
		</view>
		
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.isDefault>0?true:false" color="#fa436a" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm">提交</button>
		<address-picker @confirm="getPosName" :show="showAddrPicker" @cancel="showAddrPicker=false" ></address-picker>
	</view>
</template>

<script>
	import {apiAddAddr, apiEditAddr} from '@/api/address.js'
	export default {
		data() {
			return {
				showAddrPicker:false,
				addressData: {
					name: '',
					phone: '',
					posName: '在地图选择',
					posAddress: '',
					longitude:0,
					latitude:0,
					isDefault:0
				}
			}
		},
		onLoad(option){
			let title = '新增收货地址';
			if(option.type==='edit'){
				title = '编辑收货地址'
				this.addressData = JSON.parse(option.data)
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			switchChange(e){
				console.log(e)
				this.addressData.isDefault = e.detail?1:0;
			},
			getPosName(e){
				
				this.showAddrPicker = false
				this.addressData.posName = e.value[0]+' '+e.value[1]+' '+e.value[2]
			},
			closeAddrPicker(){
				this.showAddrPicker = false
			},
			
			//地图选择地址
			chooseLocation(){
				uni.chooseLocation({
					success: (res)=> {
						// this.addressData.posName = res.name;
						this.addressData.posAddress = res.address;
						this.addressData.longitude = res.longitude
						this.addressData.latitude = res.latitude
					}
				})
			},
			
			//提交
			confirm(){
				let data = this.addressData;
				if(!data.name){
					this.$modal.msg('请填写收货人姓名');
					return;
				}
				if(!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.phone)){
					this.$modal.msg('请输入正确的手机号码');
					return;
				}
				if(!data.posName){
					this.$modal.msg('请选择所在地区');
					return;
				}
				if(!data.posAddress){
					this.$modal.msg('请在地图中选择位置');
					return;
				}
				let params = JSON.parse(JSON.stringify(data))
				if(this.addressData.id){
					apiEditAddr(params).then(res=>{
						console.log(111)
						this.$modal.msg(res.msg)
						this.$tab.redirectTo("/pages/address/index")
					})
				}else{
					apiAddAddr(params).then(res=>{
						this.$modal.msg(res.msg)
						this.$tab.redirectTo("/pages/address/index")
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	page{
		background: #f8f8f8;
		padding-top: 16upx;
	}

	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
		
		.tit{
			flex-shrink: 0;
			width: 120upx;
			font-size: 30upx;
			color: #303133;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: #303133;
		}
		.icon-shouhuodizhi{
			font-size: 36upx;
			color: #909399;
		}
	}
	.default-row{
		margin-top: 16upx;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: 32upx;
		color: #fff;
		background-color: #ecdd00;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 100, 96, 0.4);
	}
</style>
